package com.huarun.module.sys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.huarun.common.BaseServiceImpl;
import com.huarun.common.util.DateUtils;
import com.huarun.common.util.HttpRequest;
import com.huarun.common.util.PropertyUtil;
import com.huarun.faseplus.Data;
import com.huarun.faseplus.SubjectBean;
import com.huarun.module.sys.bean.Subject;
import com.huarun.module.sys.dao.SubjectDao;
import com.huarun.module.sys.service.ISubjectService;

/**
 * 
 * @author eva
 * @catedDate 2019-03-22
 * @description face++数据同步
 *
 */
@Service("subjectService")
public class SubjectServiceImpl extends BaseServiceImpl<SubjectDao, Subject> implements ISubjectService {

	@Resource
	private SubjectDao subjectDao;



	@Override
	public Integer inserBatch(List<Subject> subjectList) {
		// TODO Auto-generated method stub
		return subjectDao.inserBatch(subjectList);
	}
	@Override
	public boolean synSubjectList() {
		boolean isOk = false;
		try {
			HttpRequest hq = new HttpRequest();
			String uri = PropertyUtil.getProperty("face_url_B");
			String url = uri + PropertyUtil.getProperty("appLogin");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("username", PropertyUtil.getProperty("face_username"));
			map.put("password", PropertyUtil.getProperty("face_password"));
			String[] result = hq.sendPost(url, map);
			System.out.println(result[0]);
			System.out.println("##########");
			System.out.println(result[1]);
			String cookie = result[1];
			String subjectListurl = uri + PropertyUtil.getProperty("getSubjectList");
			Map<String, Object> params = new HashMap<>();
			// 'employee' - 员工; 'visitor' - 所有访客; 'vip' - VIP访客
			params.put("category", "employee");
			params.put("name", "");
			params.put("order", "");
			params.put("page", 1);
			params.put("size", 10);
			String resultStr = hq.sendGet(subjectListurl, params, cookie);
			System.out.println("获取到的Json传信息  " + resultStr);
			if (null != resultStr) {
				// SubjectBean subject = JSON.parseObject(resultStr, SubjectBean.class);//
				// jsonString转为java对象
				SubjectBean subject = JSON.parseObject(resultStr, new TypeReference<SubjectBean>() {
				});
				if (null != subject) {
					int taotal = subject.getPage().getTotal();
					Subject subjectDb = null;
					for (int i = 1; i <= taotal; i++) {
						params.put("page", i + "");
						params.put("count", 10 + "");
						String subString = hq.sendGet(subjectListurl, params, cookie);
						if (null != subString && subString.length() > 0) {
							Map<String, Object> subMap = JSONObject.parseObject(subString);
							List<Data> data = JSON.parseObject(subMap.get("data").toString(),
									new TypeReference<List<Data>>() {
									});
							List<Subject> subjectList = new ArrayList<Subject>();
							for (int j = 0; j < data.size(); j++) {
								Data d = data.get(j);
								subjectDb = new Subject();
								subjectDb.setAvatar(d.getAvatar());
								if (null != d.getBirthday()) {
									subjectDb.setBirthday(DateUtils.StringToDate(d.getBirthday()));
								}
								subjectDb.setComeFrom(d.getComeFrom());
								subjectDb.setCompanyId(d.getCompanyId());
								subjectDb.setDepartment(d.getDepartment());
								subjectDb.setDescription(d.getDescription());
								if (null != d.getEntryDate()) {
									subjectDb.setEntryDate(DateUtils.StringToDate(d.getEntryDate()));
								}
								subjectDb.setFaceId(d.getId());
								subjectDb.setGender((short) d.getGender());
								subjectDb.setInterviewee(d.getInterviewee());
								subjectDb.setJobNumber(d.getJobNumber());
								subjectDb.setPurpose(d.getPurpose());
								subjectDb.setPhone(d.getPhone());
								subjectDb.setRealName(d.getName());
								subjectDb.setRemark(d.getRemark());
								subjectDb.setStartTime(d.getStartTime());
								subjectDb.setSyncTime(new Date());
								subjectDb.setSubjectType((short) d.getSubjectType());
								subjectDb.setTitle(d.getTitle());
								subjectDb.setVisitNotify(d.isVisitNotify());
								this.save(subjectDb);
								subjectList.add(subjectDb);
							}
						}

					}
					isOk = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			isOk = false;
		}
		return isOk;
	}

}
